package com.flyco.modules.stm.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.flyco.modules.stm.entity.StmStockCheck;
import com.flyco.modules.stm.entity.StmStockCheckItem;
import com.flyco.modules.stm.mapper.StmStockCheckItemMapper;
import com.flyco.modules.stm.mapper.StmStockCheckMapper;
import com.flyco.modules.stm.service.IStmStockCheckService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 门店盘点单
 * @Author: flyco
 * @Date:   2020-12-16
 * @Version: V1.0
 */
@DS("store")
@Service
public class StmStockCheckServiceImpl extends ServiceImpl<StmStockCheckMapper, StmStockCheck> implements IStmStockCheckService {

	@Autowired
	private StmStockCheckMapper stmStockCheckMapper;
	@Autowired
	private StmStockCheckItemMapper stmStockCheckItemMapper;
	
	@Override
	@Transactional
	public void saveMain(StmStockCheck stmStockCheck, List<StmStockCheckItem> stmStockCheckItemList) {
		stmStockCheckMapper.insert(stmStockCheck);
		for(StmStockCheckItem entity:stmStockCheckItemList) {
			//外键设置
			entity.setStockCheckId(stmStockCheck.getId());
			stmStockCheckItemMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(StmStockCheck stmStockCheck,List<StmStockCheckItem> stmStockCheckItemList) {
		stmStockCheckMapper.updateById(stmStockCheck);
		
		//1.先删除子表数据
		stmStockCheckItemMapper.deleteByMainId(stmStockCheck.getId());
		
		//2.子表数据重新插入
		for(StmStockCheckItem entity:stmStockCheckItemList) {
			//外键设置
			entity.setStockCheckId(stmStockCheck.getId());
			stmStockCheckItemMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(Long id) {
		stmStockCheckItemMapper.deleteByMainId(id);
		stmStockCheckMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			stmStockCheckItemMapper.deleteByMainId(Long.valueOf(id.toString()));
			stmStockCheckMapper.deleteById(id);
		}
	}
	
}
